package f.app.al;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SetBudget extends Activity {
	Button okButton, bCancel;
	EditText food, transport, cloths, hobby;
	TextView cat1, cat2, cat3, cat4;
	public int foodBudget, transportBudget, clothsBudget, hobbyBudget;
	Database budgetDatabase;
	String [] catList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		SharedPreferences pref = getSharedPreferences("mypref", MODE_PRIVATE);
		budgetDatabase = new Database(this);
		
		if(pref.getBoolean("firststart", true)){
		   // update sharedpreference - another start wont be the first
		   SharedPreferences.Editor editor = pref.edit();
		   editor.putBoolean("firststart", false);
		   editor.commit(); // apply changes
		   setContentView(R.layout.setbudget);
			okButton = (Button) findViewById(R.id.okButton);
			bCancel = (Button) findViewById(R.id.cancelButton);
			
			food = (EditText) findViewById(R.id.foodBudget);
			transport = (EditText) findViewById(R.id.transportBudget);
			cloths = (EditText) findViewById(R.id.clothsBudget);
			hobby = (EditText) findViewById(R.id.hobbyBudget);
			
			cat1 = (TextView) findViewById(R.id.cat1);
			cat2 = (TextView) findViewById(R.id.cat2);
			cat3 = (TextView) findViewById(R.id.cat3);
			cat4 = (TextView) findViewById(R.id.cat4);
			
			catList = getResources().getStringArray(R.array.expenseList);
			TextView cat1=(TextView)findViewById(R.id.cat1);
			cat1.setText(catList[0]);
			TextView cat2=(TextView)findViewById(R.id.cat2);
			cat2.setText(catList[1]);
			TextView cat3=(TextView)findViewById(R.id.cat3);
			cat3.setText(catList[2]);
			TextView cat4=(TextView)findViewById(R.id.cat4);
			cat4.setText(catList[3]);

			okButton.setOnClickListener(new View.OnClickListener() {

				public void onClick(View v) {
					// TODO Auto-generated method stub
					foodBudget = Integer.parseInt(food.getText().toString());
					transportBudget = Integer.parseInt(transport.getText().toString());
					clothsBudget = Integer.parseInt(cloths.getText().toString());
					hobbyBudget = Integer.parseInt(hobby.getText().toString());
					
					budgetDatabase.addIncomeEntry(foodBudget, transportBudget, clothsBudget, hobbyBudget);

					try {
						Class ourClass = Class.forName("f.app.al.Menu");
						Intent ourIntent = new Intent(SetBudget.this, ourClass);
						startActivity(ourIntent);
					} catch (ClassNotFoundException e) {
						e.printStackTrace();
					}
				}
			});

			bCancel.setOnClickListener(new View.OnClickListener() {

				public void onClick(View v) {
					// TODO Auto-generated method stub
					try {
						Class ourClass = Class.forName("f.app.al.Menu");
						Intent ourIntent = new Intent(SetBudget.this, ourClass);
						startActivity(ourIntent);
					} catch (ClassNotFoundException e) {
						e.printStackTrace();
					}
				}
			});
	}
		else{
			try {
				Class ourClass = Class.forName("f.app.al.Menu");
				Intent ourIntent = new Intent(SetBudget.this, ourClass);
				startActivity(ourIntent);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
}
}
